<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./plugins/elementui/index.css">
    <link rel="stylesheet" href="./plugins/font-awesome/css/font-awesome.min.css">
    <title>用户注册/登录</title>
    <style>

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Arial', sans-serif;
        }

        body {
            background-color: #f5f5f5;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }

        .container {
            width: 400px;
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
            /*overflow: hidden;*/
        }

        .form-container {
            padding: 40px;
            transition: all 0.6s ease-in-out;
        }

        .form-title {
            text-align: center;
            margin-bottom: 30px;
            color: #333;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-size: 14px;
        }

        .form-group input {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
            transition: border 0.3s;
        }

        .form-group input:focus {
            border-color: #4285f4;
            outline: none;
        }

        .btn {
            width: 100%;
            padding: 12px;
            background-color: #4285f4;
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .btn:hover {
            background-color: #3367d6;
        }

        .switch-form {
            text-align: center;
            margin-top: 20px;
        }

        .switch-form a {
            color: #4285f4;
            text-decoration: none;
            cursor: pointer;
        }

        .switch-form a:hover {
            text-decoration: underline;
        }

        /* 登录和注册表单切换效果 */
        #login-form {
            display: block;
        }

        #register-form {
            display: none;
        }

    </style>
</head>
<body>
    <div class="container" id="app">
        <!-- 登录表单 -->
        <div id="login-form" class="form-container">
            <h2 class="form-title">用户登录</h2>
            <form action="${pageContext.request.contextPath}/commService/login" method="post">
                <div class="form-group">
                    <label for="login-username">用户名</label>
                    <input type="text" id="login-username" name="username" placeholder="请输入用户名" required>
                </div>
                <div class="form-group">
                    <label for="login-password">密码</label>
                    <input type="password" id="login-password" name="password" placeholder="请输入密码" required>
                </div>
                <div class="form-group">
                    <label for="login-captcha">验证码</label>
                    <div style="display: flex; align-items: center;">
                        <input type="text" id="login-captcha" name="captcha" placeholder="请输入验证码" required style="flex: 1; padding: 10px;">
                        <img src="${pageContext.request.contextPath}/commService/captcha"
                             alt="验证码"
                             onclick="this.src='${pageContext.request.contextPath}/commService/captcha?'+Math.random()"
                             style="cursor: pointer; height: 40px; margin-left: 10px;" />
                    </div>
                </div>
                <button type="submit" class="btn">登录</button>
            </form>
            <div class="switch-form">
                <span>还没有账号？</span>
                <a onclick="switchToRegister()">立即注册</a>
            </div>
        </div>

        <!-- 注册表单 -->
        <div id="register-form" class="form-container">
            <h2 class="form-title">用户注册</h2>
            <form action="${pageContext.request.contextPath}/commService/register" method="post">
                <div class="form-group">
                    <label for="reg-username">用户名</label>
                    <input type="text" id="reg-username" name="username" placeholder="请输入用户名" required>
                </div>
                <div class="form-group">
                    <label for="reg-password">密码</label>
                    <input type="password" id="reg-password" name="password" placeholder="请输入密码" required>
                </div>
                <div class="form-group">
                    <label for="reg-confirm-password">确认密码</label>
                    <input type="password" id="reg-confirm-password" name="confirmPassword" placeholder="请再次输入密码" required>
                </div>
                <button type="submit" class="btn">注册</button>
            </form>
            <div class="switch-form">
                <span>已有账号？</span>
                <a onclick="switchToLogin()">立即登录</a>
            </div>
        </div>
    </div>
<script src="./js/vue.js"></script>
<script src="./plugins/elementui/index.js"></script>
<script type="text/javascript" src="./js/jquery.min.js"></script>
<script src="./js/axios-0.18.0.js"></script>

<script>
    // Vue实例
    let apps = new Vue({
        el: '#app',
        methods: {
            //  提示信息
            showTip(msg, type = 'info') {
                this.$message({
                    message: msg,
                    type: type,
                    duration: 2000,
                    showClose: false,

                });
            }
        }
    });

    //  切换到注册表单
    function switchToRegister() {
        document.getElementById('login-form').style.display = 'none';
        document.getElementById('register-form').style.display = 'block';
    }

    // 切换到登录表单
    function switchToLogin() {
        document.getElementById('register-form').style.display = 'none';
        document.getElementById('login-form').style.display = 'block';
    }

    //刷新验证码
    function refreshCaptcha() {
        let captchaImg = document.querySelector("img[alt='验证码']");
        if (captchaImg) {
            captchaImg.src = '${pageContext.request.contextPath}/commService/captcha?' + Math.random();
        }
    }

    //注册
    document.querySelector('#register-form form').addEventListener('submit', function(e) {
        e.preventDefault(); // 阻止默认提交行为

        // 获取输入值
        const username = document.getElementById('reg-username').value;
        const password = document.getElementById('reg-password').value;
        const confirmPassword = document.getElementById('reg-confirm-password').value;

        // 简单校验
        if (password !== confirmPassword) {
            apps.showTip('两次密码不一致!', 'error');
            document.querySelector('#register-form form').reset();
            return;
        }

        if (password.length < 6) {
            apps.showTip('密码不能少于6位', 'error');
            document.querySelector('#register-form form').reset();
            return;
        }

        // 使用 axios 发送 POST 请求
        axios.post('${pageContext.request.contextPath}/commService/register', {
            username: username,
            password: password
        }).then(function(response) {

            if (response.data.code === 201) {
                apps.showTip('注册成功！', 'success')
                document.querySelector('#register-form form').reset();
                switchToLogin(); // 跳转到登录界面

            } else if (response.data.code === 200) {
                document.querySelector('#register-form form').reset();
                apps.showTip('注册失败',  'error')

            } else if (response.data.code === 10001){
                document.querySelector('#register-form form').reset();
                apps.showTip(response.data.msg, 'error')

            }
        }).catch(function(error) {
            // console.error('请求异常:', error);
            document.querySelector('#register-form form').reset();
            apps.showTip('请求异常，请稍后再试', 'error')

        });
    });


    //登录
    document.querySelector('#login-form form').addEventListener('submit', function(e) {
        e.preventDefault();
        const username = document.getElementById('login-username').value;
        const password = document.getElementById('login-password').value;
        const captcha = document.getElementById('login-captcha').value;

        axios.defaults.headers.post['Content-Type'] = 'application/json;charset=utf-8';
        axios.post('${pageContext.request.contextPath}/commService/login', {
            username: username,
            password: password,
            captcha: captcha
        }).then(function(response) {

            if(response.data.code === 501){     //  登录成功
                document.querySelector('#login-form form').reset();
                window.location.href = '${pageContext.request.contextPath}/commService/userIp.do';

            }else if(response.data.code === 500){   //  登录失败
                apps.showTip(response.data.msg, 'error')
                document.querySelector('#login-form form').reset(); //清空登录列表
                refreshCaptcha();

            }else if(response.data.code === 20000){     // 验证码错误
                apps.showTip(response.data.msg, 'error')
                document.getElementById('login-password').value = '';
                document.getElementById('login-captcha').value = '';
                refreshCaptcha();

            }else if(response.data.code === 10000){     //  用户不存在
                apps.showTip(response.data.msg, 'error')
                document.querySelector('#login-form form').reset();
                document.querySelector('#register-form form').reset();  //清空注册列表
                refreshCaptcha();
                switchToRegister();

            }
        }).catch(function(error) {  // 请求异常
            console.error('请求异常:', error);
            apps.showTip('请求异常，请稍后再试',  'error')

        });

    });

</script>


</body>
</html>
